//
//  ZYChengShiViewController.m
//  XiangMuYiBai
//
//  Created by mac on 15-4-20.
//  Copyright (c) 2015年 zhiyou. All rights reserved.
//

#import "ZYChengShiViewController.h"
#import "Header.h"
@interface ZYChengShiViewController ()

@end

@implementation ZYChengShiViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIImageView *touTiao=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 60)];
    touTiao.image=[UIImage imageNamed:@"c_bg@2x.png"];
    [self.view addSubview:touTiao];
    
    UIButton *back=[UIButton buttonWithType:UIButtonTypeSystem];
    back.frame=CGRectMake(20, 20, 20, 20);
    [back setBackgroundImage:[UIImage imageNamed:@"back@2x.png"] forState:UIControlStateNormal];
    [back addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:back];
    UIScrollView* mainScroll=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 60, 320, 420)];
    mainScroll.contentSize=CGSizeMake(320, 600);
  
    mainScroll.backgroundColor=[UIColor lightGrayColor];
   
    [self.view addSubview:mainScroll];
    _smallScroll=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 200)];
    _smallScroll.contentOffset=CGPointMake(0, 0);
    _smallScroll.pagingEnabled=YES;
     _smallScroll.delegate=self;
    _smallScroll.contentSize=CGSizeMake(320*5, 200);
    for (int i =0; i<5; i++)
    {
        UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(i*320, 0, 320, 200)];
        NSString *str=[NSString stringWithFormat:@"c_item%d@2x.jpg",i];
        imageView.image=[UIImage imageNamed:str];
        [_smallScroll addSubview:imageView];
    }
    [mainScroll addSubview:_smallScroll];
    _page=[[UIPageControl alloc]initWithFrame:CGRectMake(0, 160, 320, 40)];
    _page.numberOfPages=5;
    _page.currentPage=0;
    [_page addTarget:self action:@selector(change) forControlEvents:UIControlEventValueChanged];
    _page.pageIndicatorTintColor=[UIColor redColor];
    _page.currentPageIndicatorTintColor=[UIColor yellowColor];
    [mainScroll addSubview:_page];
    array2=[[NSMutableArray alloc]init];
    for (int i = 0; i<13; i++)
    {
        int row=i/3;
        int column=i%3;
        int rowInterVal=(400-4*72)/5;
        int columnInterVal=(320-3*72)/4;
        UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(columnInterVal+column*(columnInterVal+72), rowInterVal+row*(rowInterVal+72)+200, 72, 72)];
        NSString *str=[NSString stringWithFormat:@"c_%d",i%12 +1];
        imageView.image=[UIImage imageNamed:str];
        imageView.tag=i+1;
        [array2 addObject:imageView];
        [mainScroll addSubview:imageView];
    }
   NSTimer *time = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timer) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop]addTimer:time forMode:NSRunLoopCommonModes];//让time 和UI同级
    a=-1;
    array=[[NSMutableArray alloc]init];
    for (int i=0; i<4; i++)
    {
        UIButton*btn=[UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame=CGRectMake(270, 430, 45, 45);
        [btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"c_setting%d",(i+1)%4]] forState:UIControlStateNormal];
        btn.tag=101+i;
        [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        if (btn.tag<=103)
        {
            [array addObject:btn];
        }
        
        [self.view addSubview:btn];
         point=btn.center;
    }
    
}
-(void)click:(UIButton *)btn
{
    
    if (btn.tag==104)
    {
        isFenKai=!isFenKai;
        for (UIButton *button in array)
        {
            [UIView beginAnimations:nil context:button];
            [UIView setAnimationDuration:.5];
            [UIView setAnimationRepeatCount:1];
            button.center=CGPointMake(point.x+((button.tag-104)%2 )* 70, point.y-(button.tag-100)/2*70);
            [UIView setAnimationDelegate:self];
            [UIView commitAnimations];
        }
    }
    else
    {
        UIButton *button=(UIButton*)[self.view viewWithTag:104];
        [self click:button];
        for (UIImageView *imageView in array2)
        {
            if (imageView.tag-10>=btn.tag-100)
            {
                imageView.hidden=YES;
            }
            else
            {
                imageView.hidden=NO;
            }
        }
    }

}
-(void)animationWillStart:(NSString *)animationID context:(void *)context
{

    UIButton *button=(UIButton*)context;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:.5];
    [UIView setAnimationRepeatCount:1];
    if (isFenKai)
    {
    button.center=CGPointMake(point.x+((button.tag-104)%2 )* 55, point.y-(button.tag-100)/2*55);
    }
    else
    {
        button.center=point;
    }
    [UIView commitAnimations];
}

-(void)timer
{
    if (_page.currentPage==4 ||_page.currentPage==0)
    {
        a=-a;
    }
    _page.currentPage=_page.currentPage+a;
    [_smallScroll setContentOffset:CGPointMake(_page.currentPage*320, 0) animated:YES];
}
-(void)change
{
    [_smallScroll setContentOffset:CGPointMake(_page.currentPage*320, 0) animated:YES];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    _page.currentPage=scrollView.contentOffset.x/320;
}
-(void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}













- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}







@end
